﻿var userList="/manager/admin/userTel.do";
var columns = [[
    {field:'id',title:'编号',width:50},
    {field:'userName',title:'账号',width:100},
    {field:'name',title:'姓名',width:100},
    {field:'tel',title:'电话',width:120},
    {field:'role',title:'角色名',width:200},
    {field:'depart',title:'部门名',width:200},
    {field:'status',title:'状态',width:50,formatter: function (value, row, index) {
        switch(value) {
            case 1 : return '正常';
            case 2 : return '停止';
            case 3 : return '已删除';
        }
        return '未知';}}
]];

function query(curPage, pageSize){
	
    curPage = parseInt(curPage);
    pageSize = parseInt(pageSize);

	$.get(userList, {}, function (json) {
		var code=json.code;
    	if(code!=200){
    	  $('#tt').datagrid({data:[]});
    	  return;
    	}
		var data = json.data;
		var total = data.size;
		var totalPage = Math.ceil(total / pageSize);
		var start = (curPage - 1) * pageSize;
		var dataLength = 0;
		data = data["results"];
		if (data != null) {
			dataLength = data.length;
		}
		var iHeight = document.documentElement.clientHeight;
        $('#tt').datagrid( {
            width : '100%',
            height : iHeight-85,
            fitColumns : false, //显示序号
            columns : columns,
            singleSelect:true,//是否单选 
            striped:true,//表格奇偶渐变色
            pagination: true,//打开翻页功能
            toolbar: CreateToolBar(),
            pageList: [100, 200, 500, 1000],
            data:data
        });
        
	    var p = $('#tt').datagrid('getPager');
	    $(p).pagination({
		      total: total,
		      pageList: [100, 200, 500, 1000],
		      pageSize: pageSize,
		      pageNumber: curPage,
		      beforePageText: '第',
		      afterPageText: '页    共 ' + totalPage + ' 页',
		      displayMsg: '当前显示 ' + (start + 1) + '-' + (start + dataLength) + ' 条记录 共 ' + total + ' 条记录',
		      onSelectPage: function (curPage, pageSize) {
		        query(curPage, pageSize);
		      },
		      onChangePageSize: function (ps) {
		        pageSize = ps;
		      }
	    });
	}, "json");
}

$(document).ready(function () {
    $('#fmUpdate').form( {
        success : function(data) {
            CRUD.queryList();
            try{
                var result = eval('(' + data + ')');
                if(result == true){
                    $('#dialogUpdate').dialog('close');
                } else {
                    $.messager.alert('提示', '修改失败');
                }
            }catch(err){
            }
        }
    });

    CRUD.queryList(1, 1000);
    InitForm();

    //确定更新
    $('#a_UpdateOK').click(function () {
        if (!$('#tel').val()) {
            $.messager.alert('提示', '电话不能为空！');
            return;
        }
        $('#fmUpdate').submit();
    });
    //取消
    $('#a_UpdateCancle').click(function () {
        var textarea = $('#idScales');
        if (textarea) {
            textarea.remove();
        }
        $('#fmUpdate').form('clear');
        $('#dialogUpdate').dialog('close');
        $('#tt').datagrid("clearSelections");
    });
});

var CRUD = {
    queryList: function () {
    	 query(1, 1000)
    },
    Update: function () {
        var r = $('#tt').datagrid('getSelected');
        if (r == null) {
            $.messager.alert('提示', '请选择一行数据');
            return;
        }
        $('#dialogUpdate').dialog('open');
        $('#fmUpdate').form('load', {
            id: r.id,
            userName: r.userName,
            name: r.name,
            tel: r.tel
        });
        $('#tt').datagrid("clearSelections");
        $('#dialogUpdate').dialog("setTitle", "修改用户");
    }
};

function CreateToolBar() {
    var a = [];
    var isUpdate = true;
    if (isUpdate) {
        a.push({
            text: '编辑',
            iconCls: 'icon-edit',
            handler: function () {
                CRUD.Update();
            }
        });
        a.push('-');
    }
    return a;
}

function InitForm() {
    $('#dialogUpdate').dialog(
    {
        closed: true,
        title: "编辑用户",
        width: 450,
        height: 250
    });
}

//自适应
window.onresize = function () {
	var iHeight = document.documentElement.clientHeight;
	$("#tt").datagrid('resize',{height:iHeight-85+'px'});            
}
